package com.lenovo.spider.process.downloader;

import com.lenovo.spider.domain.Request;
import com.lenovo.spider.domain.Response;
import com.lenovo.spider.domain.Task;
import com.lenovo.spider.process.TaskProcess;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;


/**
 * @author <a href="mailto:liuhan3@lenovo.com">HanL(liuhan3)</a>
 * on 2018/3/21 17:33
 */
@Slf4j
public abstract class Downloader implements TaskProcess {

    public Task download(Task task) {
        return doIf(Task::isSuccess, t -> {
            try {
                t.setResponse(doDownload(t.getRequest()));
            } catch (Exception e) {
                t.error(e);
            }
            return t;
        }).apply(task);
    }

    protected abstract Response doDownload(Request request) throws IOException;
}
